package org.phantom.securityframework.local;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.subject.Subject;
import org.phantom.securityframework.api.Account;
import org.phantom.securityframework.api.Permission;
import org.phantom.securityframework.api.RoleManager;
import org.springframework.stereotype.Service;

import org.phantom.securityframework.api.domain.Role;
import org.phantom.securityframework.auth.realm.SubjectPermissionRealm;
/**
 * 获取当前帐号所在角色的所有权限
 * @author david
 *
 */
@Service
public class RolePermissionRealm implements SubjectPermissionRealm{

	@Resource
	private RoleManager roleManager = null;
	@Override
	public List<Permission> getSubjectPermission(Subject subject) {
		if(subject==null)return null;
		
		List<Permission> allPermission = new ArrayList<Permission>();
		Account account = (Account) subject;
		for (Role role : account.getRoles()) {
			List<Permission> rolePermissions = roleManager.permissions(role);
			allPermission.addAll(rolePermissions);
		}
		return allPermission;
	}

}
